package com.rongshe.web.controller.hotel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rongshe.hotel.domain.RsGoodUnit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rongshe.common.annotation.Log;
import com.rongshe.common.core.controller.BaseController;
import com.rongshe.common.core.domain.AjaxResult;
import com.rongshe.common.enums.BusinessType;
import com.rongshe.hotel.domain.RsGood;
import com.rongshe.hotel.service.IRsGoodService;
import com.rongshe.common.utils.poi.ExcelUtil;
import com.rongshe.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author rongshe
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/hotel/good")
public class RsGoodController extends BaseController {
    @Autowired
    private IRsGoodService rsGoodService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('hotel:good:list')")
    @GetMapping("/list")
    public TableDataInfo list(RsGood rsGood) {
        startPage();
        List<RsGood> list = rsGoodService.selectRsGoodList(rsGood);
        return getDataTable(list);
    }

    @GetMapping("/all")
    public AjaxResult all(RsGood rsGood) {
        rsGood.setStatus("0");
        List<RsGood> list = rsGoodService.selectRsGoodList(rsGood);
        list.stream().forEach(good -> {
            ArrayList<RsGood.Spec> specs = new ArrayList<>();
            List<RsGoodUnit> rsGoodUnitList = good.getRsGoodUnitList();
            if (rsGoodUnitList != null && rsGoodUnitList.size() > 0) {
                rsGoodUnitList.stream().forEach(unit -> {
                    if (unit.getStatus().equals("0")) {
                        if(unit.getParentId() == 0){
                            RsGood.Spec spec = new RsGood.Spec();
                            spec.setId(unit.getId());
                            spec.setName(unit.getName());
                            spec.setOptions(new ArrayList<>());
                            specs.add(spec);
                        }else{
                            RsGood.Option option = new RsGood.Option();
                            option.setId(unit.getId());
                            option.setText(unit.getName());
                            option.setPrice(unit.getPrice());
                            RsGood.Spec spec1 = specs.stream().filter(spec -> spec.getId().equals(unit.getParentId())).findFirst().get();
                            spec1.getOptions().add(option);
                        }
                    }
                });
            }
            good.setSpecs(specs);

        });

        return AjaxResult.success(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('hotel:good:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RsGood rsGood) {
        List<RsGood> list = rsGoodService.selectRsGoodList(rsGood);
        ExcelUtil<RsGood> util = new ExcelUtil<RsGood>(RsGood.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('hotel:good:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(rsGoodService.selectRsGoodById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('hotel:good:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RsGood rsGood) {
        return toAjax(rsGoodService.insertRsGood(rsGood));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('hotel:good:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RsGood rsGood) {
        return toAjax(rsGoodService.updateRsGood(rsGood));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('hotel:good:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rsGoodService.deleteRsGoodByIds(ids));
    }
}
